﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BL.BetterControls
{

    [ToolboxData("<{0}:_DropDownList Description=\"\" DivStyle=\"\"  DivType=\"\" LabelClass=\"\" runat=\"server\"></{0}:_DropDownList>")]
    public class _DropDownList : DropDownList
    {
        public string DivType { get { return ViewState["DivType"] != null ? (string)ViewState["DivType"] != "" ? (string)ViewState["DivType"] : "none" : "none"; } set { ViewState["DivType"] = value; } }
        public string DivStyle { get { return ViewState["DivStyle"] != null ? (string)ViewState["DivStyle"] : ""; } set { ViewState["DivStyle"] = value; } }
        public string LabelClass { get { return ViewState["LabelClass"] != null ? (string)ViewState["LabelClass"] : ""; } set { ViewState["LabelClass"] = value; } }
        public string Description { get { return ViewState["Description"] != null ? (string)ViewState["Description"] : ""; } set { ViewState["Description"] = value; } }
        public int TextLength { get { return ViewState["TextLength"] != null ? (int)ViewState["TextLength"] : 0; } set { ViewState["TextLength"] = value; } }
        public bool ShowLabel { get { return ViewState["ShowLabel"] != null ? (bool)ViewState["ShowLabel"] : true; } set { ViewState["ShowLabel"] = value; } }
        
        protected override void Render(HtmlTextWriter writer)
        {

            if (TextLength > 0)
            {
                foreach(ListItem li in Items)
                {
                    li.Text = li.Text.Length > TextLength ? li.Text.Substring(0, TextLength) + "..." : li.Text;
                }
            }

            if (this.ToolTip == "")
                this.ToolTip = this.Description;

            if (DivType.ToLower() != "none")
            {
                writer.Write("<div");
                if (DivType != string.Empty)
                    writer.Write(" class=\"" + DivType + "\"");
                if (DivStyle != string.Empty)
                    writer.Write(" style=\"" + DivStyle + "\"");

                writer.Write(">");

            }

            string labelDisable = "";

            if (!this.Enabled)
                labelDisable = "disabled";

            if (ShowLabel)
            {

                if (LabelClass != String.Empty)
                    writer.Write("<label class=\"" + LabelClass + "\" " + labelDisable + " for=\"" + this.ClientID + "\" title=\"" + this.ToolTip + "\">" + this.Description + "</label>");
                else
                    writer.Write("<label for=\"" + this.ClientID + "\" " + labelDisable + " title=\"" + this.ToolTip + "\">" + this.Description + "</label>");

            }
            base.Render(writer);

            if (DivType.ToLower() != "none")
                writer.Write("</div>"); ;
        }







    }
}
